<?php

switch( @$URL[1] )
{
    case 'create':

        $_page_content .= $current_page->fetch( 'report_create.tpl' );
        break;
    case 'create-action':

        $data = json_decode( $_POST['data'] );

        $report_data = array(
            'activity'      => 1,
            'name'          => $_POST['Report_name']
            );

        $report_new = $Report_model->add( $report_data );

        $order_index = 0;

        foreach( $data as $d )
        {
            $order_index++;

            $question = array();

            my_DBG( '++++++', 1 );

            $question['question'] = $d->question;
            $question['type'] = $d->type;
            $question['order'] = $order_index;

            if ( isset( $d->answers ) )
            {
                foreach( $d->answers as $ans )
                {
                    $question['choices'][] = $ans;
                }
                $question['choices'] = json_encode( $question['choices'] );
            }

            if ( isset( $d->filters ) )
            {
                $question['filters'] = array();
                $question['filters']['email_enabled'] = $d->filters->question_edit_options_email_enabled;

                $question['filters']['charlimit_enabled'] = $d->filters->question_edit_options_charlimit_enabled;
                $question['filters']['charlimit'] = $d->filters->question_edit_options_charlimit;

                $question['filters']['charpool_enabled'] = $d->filters->question_edit_options_charpool_enabled;
                $question['filters']['charpool'] = $d->filters->question_edit_options_charpool;

                $question['filters']['minimum_enabled'] = $d->filters->question_edit_options_minimum_enabled;
                $question['filters']['minimum'] = $d->filters->question_edit_options_minimum;

                $question['filters']['maximum_enabled'] = $d->filters->question_edit_options_maximum_enabled;
                $question['filters']['maximum'] = $d->filters->question_edit_options_maximum;

                $question['filters']['minimum_date_enabled'] = $d->filters->question_edit_options_minimum_date_enabled;
                $question['filters']['minimum_date'] = $d->filters->question_edit_options_minimum_date;
                $question['filters']['minimum_date_hour'] = $d->filters->question_edit_options_minimum_date_hour;
                $question['filters']['minimum_date_minute'] = $d->filters->question_edit_options_minimum_date_minute;

                $question['filters']['maximum_date_enabled'] = $d->filters->question_edit_options_maximum_date_enabled;
                $question['filters']['maximum_date'] = $d->filters->question_edit_options_maximum_date;
                $question['filters']['maximum_date_hour'] = $d->filters->question_edit_options_maximum_date_hour;
                $question['filters']['maximum_date_minute'] = $d->filters->question_edit_options_maximum_date_minute;

                $question['filters']['observations_enabled'] = $d->filters->question_edit_options_observations_enabled;
                $question['filters']['observations'] = $d->filters->question_edit_options_observations;
            }
            else
            {
                $question['filters'] = array();
                $question['filters']['email_enabled'] = null;

                $question['filters']['charlimit_enabled'] = null;
                $question['filters']['charlimit'] = null;

                $question['filters']['charpool_enabled'] = null;
                $question['filters']['charpool'] = null;

                $question['filters']['minimum_enabled'] = null;
                $question['filters']['minimum'] = null;

                $question['filters']['maximum_enabled'] = null;
                $question['filters']['maximum'] = null;

                $question['filters']['minimum_date_enabled'] = null;
                $question['filters']['minimum_date'] = null;
                $question['filters']['minimum_date_hour'] = null;
                $question['filters']['minimum_date_minute'] = null;

                $question['filters']['maximum_date_enabled'] = null;
                $question['filters']['maximum_date'] = null;
                $question['filters']['maximum_date_hour'] = null;
                $question['filters']['maximum_date_minute'] = null;

                $question['filters']['observations_enabled'] = null;
                $question['filters']['observations'] = null;

            }

            $question['filters'] = json_encode( $question['filters'] );

            my_DBG( $question, 1 );

            $question_id = $Question->add( $report_new, $question );
            if ( $question_id )
            {
                my_DBG( 'Question added ok -> '. $question_id, 1 );
            }
            else
            {
                my_DBG( 'Question not added', 1 );
            }

        }

        break;

    case 'show':

        $report_id = (int)@$URL[2];

        $report_model_item = $Report_model->find_by( 'id', $report_id );
        $questions = $Question->find_by( 'report', $report_id );
        
        //my_DBG( $questions );

        $current_page->assign( 'report_model', $report_model_item );
        $current_page->assign( 'questions', $questions );
        $_page_content .= $current_page->fetch( 'report_show.tpl' );

        break;

    case 'list':
        
        $reports = $Report_model->get_all();
        
        $current_page->assign( 'reports', $reports );
        $_page_content .= $current_page->fetch( 'report_list.tpl' );
        
        break;
    
    case '':
    default:
        $_page_content .= $current_page->fetch( 'report.tpl' );
        break;
}

?>